package com.yunhe.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.javabean.Role;
import com.yunhe.javabean.Users;
import com.yunhe.service.RoleService;
import com.yunhe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page,limit);
        List<Users> allUser = userService.findAllUser();
        PageInfo<Users> pageInfo=new PageInfo<>(allUser);
        request.setAttribute("pageInfo",pageInfo);
        return "/users/users-list";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        //查询指定用户信息
        Users users = userService.findById(id);
        //查询所有角色信息
        List<Role> roleList = roleService.findAll();
        request.setAttribute("users",users);
        request.setAttribute("roleList",roleList);
        return "/users/users-edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Users users,int roleId){
        userService.edit(users,roleId);
        return "redirect:/users/findAll";
    }



    @RequestMapping(value = "/delete")
    public String deleteById(int id){
        userService.deleteById(id);

        return "redirect:/users/findAll";
    }


    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(HttpServletRequest request){
        //查询角色列表数据
        List<Role> roleList = roleService.findAll();
        request.setAttribute("roleList",roleList);
        return "/users/users-add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Users users,int roleId){
        userService.add(users,roleId);
        return "redirect:/users/findAll";
    }

    @RequestMapping("/name")
    public String getUsername(HttpServletRequest request){
        SecurityContext context = SecurityContextHolder.getContext();
        UserDetails userDetails= (UserDetails)context.getAuthentication().getPrincipal();
        request.getSession().setAttribute("username",userDetails.getUsername());
        return "main";
    }


}
